package com.kexinde.wjifen.Thread;

import java.util.List;

import javax.servlet.ServletContext;

import org.apache.log4j.Logger;

import com.kexinde.wjifen.dao.ActivityDao;
import com.kexinde.wjifen.dao.CommentDao;
import com.kexinde.wjifen.dao.GiftDao;
import com.kexinde.wjifen.dao.ShopDao;
import com.kexinde.wjifen.dao.ShopProductDao;
import com.kexinde.wjifen.init.IndexInit;
import com.kexinde.wjifen.model.Activity;
import com.kexinde.wjifen.model.Gift;
import com.kexinde.wjifen.model.Shop;
import com.kexinde.wjifen.model.ShopProduct;
import com.kexinde.wjifen.util.OSCache;
import com.kexinde.wjifen.vo.IndexComment;


/**
 * @author vincent
 *
 */
public class IndexThread implements Runnable {

	private final static Logger logger = Logger.getLogger(IndexInit.class);
	ServletContext application = null;
	
	/**
	 * 有参构造方法
	 * @param application
	 * application对象
	 */
	public IndexThread(ServletContext application){
		this.application = application;
	}
	
	
	@SuppressWarnings("unchecked")
	@Override
	public void run() {
		logger.info("首页线程启动");
		int time = 300;	//缓存时间
		while (true) {
			try {
				List<Shop> hotShops = (List<Shop>) OSCache
						.getInstance().get("hotShops");
				
				List<Shop> newShops = (List<Shop>) OSCache
						.getInstance().get("newShops");
				
				List<Activity> activities = (List<Activity>) OSCache
						.getInstance().get("activities");
				
				List<IndexComment> comments = (List<IndexComment>) OSCache
						.getInstance().get("comments");
				
				List<ShopProduct> shopProducts = (List<ShopProduct>) OSCache
						.getInstance().get("shopProducts");
				
				List<Gift> gifts = (List<Gift>) OSCache
						.getInstance().get("gifts");
				
				List<Gift> newGifts = (List<Gift>) OSCache
				.getInstance().get("newGifts");

				if (hotShops == null) { // 缓存过期，重新查询数据库，再写缓存
					logger.info("hotShops缓存过期，重新查询数据库");
					hotShops = this.getHotShops();
					OSCache.getInstance()
							.inputOSCache("hotShops", hotShops, time);
					application.setAttribute("hotShops", hotShops);
				}
				
				if (newShops == null) { // 同上
					logger.info("newShops缓存过期，重新查询数据库");
					newShops = this.getNewShops();
					OSCache.getInstance()
							.inputOSCache("newShops", newShops, time);
					application.setAttribute("newShops", newShops);
				}
				
				if (activities == null) { // 同上
					logger.info("activities缓存过期，重新查询数据库");
					activities = this.getActivities();
					OSCache.getInstance().inputOSCache("activities",
							activities, time);
					application.setAttribute("activities", activities);
				}
				
				if (comments == null) { // 同上
					logger.info("comments缓存过期，重新查询数据库");
					comments = this.getComments();
					OSCache.getInstance()
							.inputOSCache("comments", comments, time);
					application.setAttribute("comments", comments);
				}
				
				if (shopProducts == null) { // 同上
					logger.info("shopProducts缓存过期，重新查询数据库");
					shopProducts = this.getShopProducts();
					OSCache.getInstance()
					.inputOSCache("shopProducts", shopProducts, time);
					application.setAttribute("shopProducts", shopProducts);
				}
				
				if (gifts == null) { // 同上
					logger.info("gifts缓存过期，重新查询数据库");
					gifts = this.getWjifenGifts();
					OSCache.getInstance()
					.inputOSCache("gifts", gifts, time);
					application.setAttribute("gifts", gifts);
				}
				
				if (newGifts == null) { // 同上
					logger.info("newGifts缓存过期，重新查询数据库");
					newGifts = this.getNewGifts();
					OSCache.getInstance()
					.inputOSCache("newGifts", newGifts, time);
					application.setAttribute("newGifts", newGifts);
				}

				logger.info("成功装入首页数据到APP");
				
				
				logger.info("进入线程轮训,睡眠120秒");
				
				
				Thread.sleep(120000);
				
				
			} catch (Exception e) {
				logger.error(e);
			}
		}
	}
	
	
	
	
	/**
	 * 获取16个热门商店
	 * 
	 * @return
	 */
	private List<Shop> getHotShops() {
		try {
			ShopDao shopDao = new ShopDao();
			return shopDao.queryHostShops();
		} catch (Exception e) {
			logger.error(e);
		}
		return null;
	}

	/**
	 * 获取16个最新商店
	 * 
	 * @return
	 */
	private List<Shop> getNewShops() {
		try {
			ShopDao shopDao = new ShopDao();
			return shopDao.queryNewShops();
		} catch (Exception e) {
			logger.error(e);
		}
		return null;
	}

	/**
	 * 获取首页11个商家活动
	 * 
	 * @return
	 */
	private List<Activity> getActivities() {
		try {
			ActivityDao activityDao = new ActivityDao();
			return activityDao.queryIndexActivities();
		} catch (Exception e) {
			logger.error(e);
		}
		return null;
	}
	
	/**
	 * 获取首页最新点评
	 * @return
	 */
	private List<IndexComment> getComments() {
		try {
			CommentDao commentDao = new CommentDao();
			return commentDao.queryIndexComments();
		} catch (Exception e) {
			logger.error(e);
		}
		return null;
	}
	
	
	/**
	 * 获取首页热门微商家兑换
	 * @return
	 */
	private List<ShopProduct> getShopProducts() {
		try {
			ShopProductDao shopProductDao = new ShopProductDao();
			return shopProductDao.queryIndexShopProducts();
		} catch (Exception e) {
			logger.error(e);
		}
		return null;
	}
	
	
	/**
	 * 获取首页热门微商城兑换(即微积分商城礼品)
	 * @return
	 */
	private List<Gift> getWjifenGifts() {
		try {
			GiftDao GiftDao = new GiftDao();
			return GiftDao.queryIndexGifts();
		} catch (Exception e) {
			logger.error(e);
		}
		return null;
	}
	
	
	/**
	 * 获取list页面中9个最新商城商品
	 * @return
	 */
	private List<Gift> getNewGifts() {
		try {
			GiftDao GiftDao = new GiftDao();
			return GiftDao.queryNewGifts();
		} catch (Exception e) {
			logger.error(e);
		}
		return null;
	}


}
